<?php
$section = 'banner';
$titre = _LB_BANNER_MANAGEMENT;
$smarty->assign("titre", $titre);

//get object ID
$id = Request::getIntNumber('id');
$act = Request::getIntNumber('act');

//get back link
$backlink = Session::getBackLink($section,"list/?");

$module = Module::getList();

$arr_modules = array();
$banner_module = new Banner_Module();

$banner = new Banner();
if($id)
{
	if(!$banner->get($id))
		redirect($backlink);
	
	$arr_modules = $banner_module->arrayBanner($id);
}

//if action = UPDATE or CREATE
if ($act == ACT_VALID_EDIT)
{
	$banner->NAME = trim(Request::get('NAME'));
    $banner->DISABLED = Request::getNumber('DISABLED');
	$arr_modules = Request::get('ID_MODULE');

	if ($banner->validateInput())
	{
		if(!$banner->ID) {
			$banner->insert();
        }
		else {
			$banner_module->deleteBanner($banner->ID);
			$banner->update();
        }
		$banner_module->insertBanner($banner->ID, $arr_modules);

		redirect($backlink);
	}
}

include_once CKEDITOR_BASE_DIR. "ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = CKEDITOR_BASE_PATH;
$CKEditor->config['height'] = 300;
$CKEditor->config['filebrowserBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html';
$CKEditor->config['filebrowserImageBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Images';
$CKEditor->config['filebrowserFlashBrowseUrl'] = CKFINDER_BASE_PATH.'ckfinder.html?type=Flash';
$CKEditor->config['filebrowserUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Files';
$CKEditor->config['filebrowserImageUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Images';
$CKEditor->config['filebrowserFlashUploadUrl'] = CKFINDER_BASE_PATH.'core/connector/php/connector.php?command=QuickUpload&type=Flash';
//create cfer
$cfer = new Cfer(array(
	_LB_ACCUEIL => Utils::getHomeLink(),
	_LB_BANNER_MANAGEMENT => $backlink,
	($id == 0? _LB_ADD_BANNER : _LB_EDIT_BANNER) => Utils::getCurrentLink() ));

//if validate is false, display form again
$smarty->assign('cfer', $cfer);
$smarty->assign('act', $act);
$smarty->assign('banner', $banner);
$smarty->assign('module', $module);
$smarty->assign('arr_modules', $arr_modules);
$smarty->assign('backlink', $backlink);
$smarty->assign('CKEditor', $CKEditor);
$smarty->display('banner_form.html');
